package com.yuxi.Util;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;   
import java.util.ArrayList; 
import java.util.List;   
import java.util.Map;

import com.yuxi.excelEntity.OwnWordInfo;

import freemarker.template.Configuration;   
import freemarker.template.Template;   
import freemarker.template.TemplateException;   
    
public class ExportReSeachWord {   
    private Configuration configuration = null; 
    private String outFilePath;
    private String realPath;
    
    public ExportReSeachWord(String outFilePath,String name) {   
        configuration = new Configuration();     
        this.configuration.setDefaultEncoding("UTF-8");       
        this.outFilePath=outFilePath;
        this.realPath=name;
    }   
    
    public void createDoc(Map dataMap) { 
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，   
        //这里我们的模板是放在com.havenliu.document.template包下面   
        this.configuration.setDefaultEncoding("UTF-8"); 
    	configuration.setClassForTemplateLoading(this.getClass(), "/com/yuxi/tem");   
        Template t=null;   
        try {   
            //test.ftl为要装载的模板   
            t = configuration.getTemplate("taskResNew.xml");
            t.setEncoding("UTF-8");
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        //输出文档路径及名称   
        File outFile1 = new File(this.outFilePath); 
        File outFile = new File(this.outFilePath+this.realPath); 
        try{
        	if(!outFile1 .exists() && !outFile1 .isDirectory())     
        	{ 
        		outFile1 .mkdir();    
        	}
        }catch(Exception e){
        	outFile1 .mkdir(); 
        }
        Writer out = null;   
        try { 
        	
            try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
				
            } catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}         
           
        } catch (FileNotFoundException e1) {   
            e1.printStackTrace();   
        }   
    
        try { 
        	t.setOutputEncoding("UTF-8");
            t.process(dataMap, out);   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }  
    
    
    
    /**  
     * 注意dataMap里存放的数据Key值要与模板中的参数相对应  
     * @param dataMap  
     */  
     public  Map getData(Map dataMap)   
      {   
          dataMap.put("prison", "张三");
          dataMap.put("userName", "张三");
          dataMap.put("address", "这是测试备注信息");
          dataMap.put("pNames", "这是测试备注信息"); 
          dataMap.put("list1", "这是测试备注信息"); 
          dataMap.put("list2", "这是测试备注信息"); 
          dataMap.put("list3", "这是测试备注信息"); 
         
    
        return dataMap;
      }

	
	
    public void createDoc(Map dataMap, ByteArrayOutputStream os) { 
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，   
        //这里我们的模板是放在com.havenliu.document.template包下面   
        configuration.setClassForTemplateLoading(this.getClass(), "/com/yuxi/tem");   
        Template t=null;   
        try {   
            //test.ftl为要装载的模板   
            t = configuration.getTemplate("taskResNew.xml");
            t.setEncoding("UTF-8");
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        //输出文档路径及名称          
        Writer out = null;   
        try {
			out = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}     
        try {   
        	t.setOutputEncoding("UTF-8");
            t.process(dataMap, out);   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }  
}   